#!/bin/perl -w
my $res;
my @parts;
my $i;
# replace Server with process name below
$res=`jps | grep Server`;
print($res);
@parts = split(" ",$res);
print $parts[0];

#diagnostic, repetitions, interval

if ($#ARGV != 2 ) {
  print "\n\nUSAGE: probe.pl diagnostic(jstack/jmap-live/jmap-all) repetitions(int) interval(in secs)\n\n e.g. ./probe.pl jstack 10 10\n e.g. ./probe.pl jmap-live 10 100 \n";
  exit;
}

my $diag = $ARGV[0];
my $reps = $ARGV[1];
my $inter = $ARGV[2];
my $cmd;

if ($diag eq "jstack") {
  $cmd = "/opt/sun/jdk/java/bin/jstack";
  for ($i=1; $i<=$reps; $i++) {
    $res =`$cmd -F $parts[0] > $i\.$diag`;
    print "running "; 
    print "$cmd -F $parts[0] > $i\.$diag";	
    print $res;
    sleep $inter;
  }
} else {

  if ($diag eq "jmap-live") {
    $cmd = "/opt/sun/jdk/java/bin/jmap -F -dump:live,format=b,file=";
  }

  if ($diag eq "jmap-all") {
    $cmd = "/opt/sun/jdk/java/bin/jmap -F -dump:format=b,file=";
  }

  for ($i=1; $i<=$reps; $i++) {
    print "running $cmd$i$diag $parts[0]";
    print "$cmd $parts[0]";
    $res =`$cmd$i$diag $parts[0]`;
    print $res;
    sleep $inter;
  }

}


